package com.ntt.benew.dictionary

import android.app.Application
import android.app.Service
import android.graphics.PixelFormat
import android.view.WindowManager
import com.benew.ntt.player.NttPlayCtrl
import com.benew.ntt.ydeval.view.EvalManager
import com.ntt.benew.dictionary.listener.IViewCallback
import com.ntt.benew.dictionary.view.BaseView
import com.ntt.benew.dictionary.view.DictSplitWordView
import com.ntt.benew.dictionary.view.DictWordDetailView
import com.ntt.core.nlogger.NLogger

/**
 * Created by Android Studio.
 * User: tw
 * Date: 2023/10/31
 * Time: 11:28
 */
object NttDictionaryManager : IViewCallback {

    private lateinit var mApplication: Application

    var authToken: String? = "null"
        private set
    var deviceVersion: String? = ""
        private set
    var deviceId: String? = ""
        private set

    /**
     * 查句分析 ui层
     */
    private var mDictSplitWordView: DictSplitWordView? = null

    /**
     * 单词详情 ui 层
     */
    private var mDictWordDetailView: DictWordDetailView? = null

    /**
     * window 管理器
     */
    private var mWindowManager: WindowManager? = null

    private lateinit var mWindowLayoutParams: WindowManager.LayoutParams

    fun init(application: Application) {
        this.mApplication = application

//        NttPlayCtrl.init(application)
        EvalManager.init(application)
        initLayoutParams()

    }

    /**
     * 使用前必须要传入设备token，否则接口无法调用成功
     */
    fun initRequestHeader(token: String, devVer: String, devId: String) {
        authToken = token
        deviceVersion = devVer
        deviceId = devId
    }


    /**
     * 初始化弹窗属性
     */
    private fun initLayoutParams() {
        mWindowLayoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    or WindowManager.LayoutParams.FLAG_FULLSCREEN
                    or WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,
            PixelFormat.TRANSLUCENT
        )
        mWindowLayoutParams.softInputMode =
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    }

    /**
     * 开始拆分句子
     * 调用会显示拆分句子的弹窗包含GPT分析
     */
    fun startSplitSentence(sentence: String?) {
        if (authToken.isNullOrBlank() || deviceVersion.isNullOrBlank()) {
            throw RuntimeException("缺少请求头参数，请调用 NttDictionaryManager.initRequestHeader() 传入")
        }
        if (mDictSplitWordView == null) {
//            NLogger.d(TAG, "第一次显示，view为空")
//            initView()
            mDictSplitWordView = DictSplitWordView(mApplication)
            mDictSplitWordView!!.mICallback = this
        }
        show(false)
        sentence?.let {
            mDictSplitWordView!!.startTranslate(it)
        }
    }

    /**
     * 开始分析单词
     * @param word 要分析的单词
     */
    fun startAnalyseWord(word: String?) {
        if (authToken.isNullOrBlank() || deviceVersion.isNullOrBlank()) {
            throw RuntimeException("缺少请求头参数，请调用 NttDictionaryManager.initRequestHeader() 传入")
        }
        if (mDictWordDetailView == null) {
//            NLogger.d(TAG, "第一次显示，view为空")
//            initView()
            mDictWordDetailView = DictWordDetailView(mApplication)
        }
        NttPlayCtrl.stop()
        show(true)
        mDictWordDetailView!!.setData(word)
    }


    /**
     * 显示弹窗
     */
    private fun show(isShowWordDetailView: Boolean) {
        if (mWindowManager == null) {
            mWindowManager = mApplication.getSystemService(Service.WINDOW_SERVICE) as WindowManager
        }

        if (isShowWordDetailView){
            mDictWordDetailView?.let {
                it.mICallback = this
                if (!it.isAttachedToWindow) {
                    mWindowManager?.addView(it, mWindowLayoutParams)
                } else {//重新设置mWindowLayoutParams，是为了唤醒屏幕
                    it.layoutParams = mWindowLayoutParams
                }
            }
        }else{
            mDictSplitWordView?.let {
                it.mICallback = this
                if (!it.isAttachedToWindow) {
                    mWindowManager?.addView(it, mWindowLayoutParams)
                } else {//重新设置mWindowLayoutParams，是为了唤醒屏幕
                    it.layoutParams = mWindowLayoutParams
                }
            }
        }
//        mView?.let {
//            if (mView?.isAttachedToWindow != true) {
//                mWindowManager?.addView(it, mWindowLayoutParams)
//            } else {//重新设置mWindowLayoutParams，是为了唤醒屏幕
//                it.layoutParams = mWindowLayoutParams
//            }
////            if (notStarted) {
////                it.beforeEval(entity)
////            } else {
////                it.startEval(entity)
////            }
//        }
    }

    /**
     * 弹窗消失
     */
    override fun dismiss(view: BaseView) {
//        NLogger.d(TAG, "评测弹窗消失")
        try {
            if (view.isAttachedToWindow) {
                view.mICallback = null
                mWindowManager?.removeViewImmediate(view)
            }
        } catch (e: Exception) {
//            NLogger.d(TAG, "评测弹窗消失异常", e.message)
        }
    }

}